import ajaxMainTableData from './ajaxMainTableData';
import updateButtonStatus from "./updateButtonStatus";

export default function getMainTableData() {
    const {
        searchModalValue,
        currentSelectTreeNode,
        orgVal,
        queryOid,
        include_child_depts,
        include_child_orgs,
        include_child_psncl,
        show_all_info,
        show_on_job_psn,
        pageInfo
    } = this.state;
    if (!orgVal || !orgVal.refpk) {
        this.cleanMainTableData();
        return;
    }
    let postData = {
        ...searchModalValue,
        tree_node_id: Array.isArray(currentSelectTreeNode)? currentSelectTreeNode[0]: currentSelectTreeNode,
        node_type: "ORG_NODE",
        pk_org: orgVal.refpk,
        oid: queryOid,
        func_type: 3,
        include_child_depts,
        include_child_orgs,
        include_child_psncl,
        show_all_info,
        show_on_job_psn,
        pageInfo: pageInfo
    };

    return ajaxMainTableData(postData)
        .then((res) => {
            if (res.success) {
                if (!res.data) {
                    res.data = {
                        'ncc60071702list': {
                            rows: []
                        }
                    };
                }
                res.data['ncc60071702list'].rows.map((item) => {
                    if (item.values['showorder'].value === '9999999') {
                        item.values['showorder'].value = '';
                    }
                });
                const resPageInfo = res.data['ncc60071702list'].pageInfo;
                const pks = res.data['ncc60071702list'].allpks;
                if (resPageInfo) {
                    res.data['ncc60071702list'].allpks = pks.slice(resPageInfo.pageIndex * resPageInfo.pageSize, (parseInt(resPageInfo.pageIndex) + 1) * resPageInfo.pageSize);
                    /*this.setState({
                        pageInfo: resPageInfo
                    })*/
                }
                this.setState({
                    hasSearched: true,
                    pageInfo: res.data['ncc60071702list'].pageInfo || {
                        ...pageInfo,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    },
                    exportAllpks: pks,
                    showSubsetDisabled: !(res.data['ncc60071702list'] && res.data['ncc60071702list'].rows && res.data['ncc60071702list'].rows.length > 0)
                }, () => {
                    updateButtonStatus.call(this);
                });
                this.props.table.setAllTableData('ncc60071702list', {
                    rows: res.data['ncc60071702list'].rows
                });
                if (res.data['ncc60071702list'].rows.length) {
                    this.props.table.setClickRowIndex('ncc60071702list', {
                        record: res.data['ncc60071702list'].rows[0].values,
                        index: 0
                    });
                    this.props.table.focusRowByIndex('ncc60071702list', 0);
                    this.setState({
                        focusedRow: 0
                    })
                }
            }
        });
}
